﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsCreate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.KeyPreview = true;
            InitializeComponent();
            log_lab.Text = "启动测试";

          
            //this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);   
            this.lowerBtn.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pDraw_MouseDown);
            this.lowerBtn.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pDraw_MouseUp);
            //this.KeyDown += new KeyEventHandler(tbIP1_KeyDown);
            //this.lowerBtn.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log_lab.Text = "顶部按钮点击";
            Task task = new Task(DownLoadFile_My);
            task.Start();
        }

        private void lowerBtn_Click(object sender, EventArgs e)
        {
            log_lab.Text = "底部按钮点击";
            Task task = new Task(lowerBtn_my);
            task.Start();
        }

        void DownLoadFile_My()
        {
            MethodInvoker mi = new MethodInvoker(() =>
            {
                log_lab.Text = "顶部测试";
            });
            this.BeginInvoke(mi);

        }

        void lowerBtn_my()
        {
            MethodInvoker mi = new MethodInvoker(() =>
            {
                log_lab.Text = "底部测试";
            });
            this.BeginInvoke(mi);
        }

        //  改变位置
        private void pDraw_MouseDown(object sender, MouseEventArgs e)
        {
            log_lab.Text = "顶部测试，鼠标按下";
        }

        private void pDraw_MouseUp(object sender, MouseEventArgs e)
        {
            log_lab.Text = "顶部测试，鼠标放松";
            Console.Write("键盘点了一次");
            Point point = new Point(button1.Location.X, button1.Location.Y);
            point.Y = point.Y + 10;
            button1.Location = point;
        }


        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            Point point = new Point(button1.Location.X, button1.Location.Y);
            if (e.Shift && e.KeyCode == Keys.Up)     // Ctrl + Up
            {
                point.Y = point.Y + 10;
                button1.Location = point;
            }
            else if (e.Shift && e.KeyCode == Keys.Down)    // Ctrl + Down
            {
                point.Y = point.Y - 10;
                button1.Location = point;
            }
            else if (e.Control && e.KeyCode == Keys.Left)    // Ctrl + Left
            {
                point.X = point.X - 10;
                button1.Location = point;
            }
            else if (e.Control && e.KeyCode == Keys.Right)    // Ctrl + Right
            {
                point.X = point.X + 10;
                button1.Location = point;
            }
        }

        private void tbIP1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    MessageBox.Show("您按下了回车键");
                    break;
                default:
                    break;
            }
        }

        private void winFormKeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Point point = new Point(button1.Location.X, button1.Location.Y);
            if (e.KeyCode == Keys.Up)     // Ctrl + Up
            {
                point.Y = point.Y - 10;
                button1.Location = point;
            }
            else if (e.KeyCode == Keys.Down)    // Ctrl + Down
            {
                point.Y = point.Y + 10;
                button1.Location = point;
            }
            else if (e.KeyCode == Keys.Left)    // Ctrl + Left
            {
                point.X = point.X - 10;
                button1.Location = point;
            }
            else if ( e.KeyCode == Keys.Right)    // Ctrl + Right
            {
                point.X = point.X + 10;
                button1.Location = point;
            }
            else
            {
                log_lab.Text += e.KeyCode.ToString();
            }
        }

        /// <summary>
        /// 重写系统Key处理事件
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {

            if (keyData == Keys.Up || keyData == Keys.Down ||
                keyData == Keys.Left || keyData == Keys.Right)
            {
                return false;
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }

    }
}
